<template>
  <div>
     <Row>
        <form enctype="multipart/form-data">
              <input type="file" icon="ios-download-outline" @change="onFileChange">
              <Button type="ghost" icon="ios-cloud-upload-outline" @click="onImport" >导入Excel</Button>
        </form>
       
        <input v-model="jackpotId" placeholder="jackpotId" ></input>
        <input v-model="type" placeholder="type" ></input>
        <Button type="primary" @click="onSearch">查询</Button>
        <Button type="primary" size="large" @click="onExport"  icon="ios-download-outline"></Icon>导出EXCEL</Button>
    </Row>
    <Table ref="table" class="bottom-table" :border="true" :stripe="true" :show-header="true" :data="tableData" :columns="tableColumns" ></Table>
    <Page class="bottom-page" :total="totalPage" :current="crtPage" :page-size="pageSize" show-total show-elevator @on-change="onChange"></Page>
    <Modal
        title="编辑gamejackpot"
        v-model="editGameJackPotModal" ok-text="提交" @on-ok="onSubmit">
      <Form  label-position="left" :model="formEdit" :label-width="150">
        <Form-item label="jackpotId">
            <Input v-model="formEdit.jackpotId" disabled ></Input>
        </Form-item>
        <Form-item label="type">
            <Input v-model="formEdit.type" disabled></Input>
        </Form-item>
        <Form-item label="rate">
            <Input v-model="formEdit.rate"></Input>
        </Form-item>
        <Form-item label="clientRate">
            <Input v-model="formEdit.clientRate"></Input>
        </Form-item>
        <Form-item label="minRate">
            <Input v-model="formEdit.minRate"></Input>
        </Form-item>
        <Form-item label="maxRate">
            <Input v-model="formEdit.maxRate"></Input>
        </Form-item>
        <Form-item label="initBaseJackpot">
            <Input v-model="formEdit.initBaseJackpot"></Input>
        </Form-item>
        <Form-item label="rewardRate">
            <Input v-model="formEdit.rewardRate"></Input>
        </Form-item>
      </Form>
    </Modal>
  </div>
</template>
<script>

import {getGameJackPots, getAllGameJackPots, modifyGameJackPot, importGameJackPots} from '@/api/game.js'
import {getVO} from '@/common/proto-vo.js'
import {exportExcel, importExcel} from '@/common/excelUtil.js'
export default {
  data () {
    return {
      tableData: [],
      jackpotId: '',
      filename: 'game_jack_pot',
      type: '',
      crtPage: 1,
      totalPage: 0,
      pageSize: 10,
      editGameJackPotModal: false,
      results: '',
      formEdit: {
        jackpotId: '',
        type: '',
        rate: '',
        clientRate: '',
        minRate: '',
        maxRate: '',
        initBaseJackpot: '',
        rewardRate: ''
      }
    }
  },
  created () {
    this.onSearch()
  },
  methods: {
    onSearch () {
      let tJackpotId = 0
      if (this.jackpotId !== '') {
        tJackpotId = parseInt(this.jackpotId)
      }

      let tType = 0
      if (this.type !== '') {
        tType = parseInt(this.type)
      }

      console.log('tJackpotId:', tJackpotId, 'tType:', tType)
      getGameJackPots(tJackpotId, tType, this.crtPage, this.pageSize).then((data) => {
        if (!data) {
          return
        }
        this.tableData = []
        let GameJackPotVO = getVO('GameJackPotVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = GameJackPotVO.decode(pv.data[i])
          vo.initBaseJackpot = vo.initBaseJackpot.toNumber()
          vo.rate = vo.rate.toFixed(5)
          vo.clientRate = vo.clientRate.toFixed(5)
          this.tableData.push(vo)
        }

        this.totalPage = pv.intValues[1]
        console.log(this.tableData)
      })
    },
    onChange (item) {
      console.log(item)
      this.crtPage = item
      this.onSearch()
    },
    onUploadSuccess (response, file, fileList) {
      this.$Message.info('load success!!!')
      console.log(response)
      console.log(file)
      console.log(fileList)
    },
    showEditGameJackPot (index) {
      let data = this.tableData[index]
      this.formEdit.jackpotId = data.jackpotId
      this.formEdit.type = data.type
      this.formEdit.rate = data.rate
      this.formEdit.clientRate = data.clientRate
      this.formEdit.minRate = data.minRate
      this.formEdit.maxRate = data.maxRate
      this.formEdit.initBaseJackpot = data.initBaseJackpot
      this.formEdit.rewardRate = data.rewardRate

      this.editGameJackPotModal = true
    },
    onSubmit () {
      if (this.formEdit.jackpotId === '' || this.formEdit.type === '' || this.formEdit.rate === '' || this.formEdit.clientRate === '' || this.formEdit.minRate === '' || this.formEdit.maxRate === '' || this.formEdit.initBaseJackpot === '' || this.formEdit.rewardRate === '') {
        this.$Message.error('编辑参数不能为空!!!')
        return
      }

      let GameJackPotVO = getVO('GameJackPotVO')
      var gameJackPotVO = new GameJackPotVO()

      gameJackPotVO.jackpotId = parseInt(this.formEdit.jackpotId)
      gameJackPotVO.type = parseInt(this.formEdit.type)
      gameJackPotVO.clientRate = parseInt(this.formEdit.clientRate)
      gameJackPotVO.minRate = parseInt(this.formEdit.minRate)
      gameJackPotVO.maxRate = parseInt(this.formEdit.maxRate)
      gameJackPotVO.initBaseJackpot = Number(this.formEdit.initBaseJackpot)
      gameJackPotVO.rewardRate = parseInt(this.formEdit.rewardRate)

      modifyGameJackPot(gameJackPotVO).then((data) => {
        if (!data) {
          return
        }
        let success = data.data.data.intValues[0]
        if (success === 1) {
          let len = this.tableData.length
          for (let i = 0; i < len; i++) {
            let data = this.tableData[i]
            if (data.jackpotId === this.formEdit.jackpotId && data.type === this.formEdit.type) {
              this.tableData[i].rate = this.formEdit.rate
              this.tableData[i].clientRate = this.formEdit.clientRate
              this.tableData[i].minRate = this.formEdit.minRate
              this.tableData[i].maxRate = this.formEdit.maxRate
              this.tableData[i].initBaseJackpot = this.formEdit.initBaseJackpot
              this.tableData[i].rewardRate = this.formEdit.rewardRate
              break
            }
          }
        }
      })
    },
    onImport () {
      // 导入EXCEL
      importGameJackPots(this.results).then((data) => {
        if (!data) {
          return
        }
        let success = data.data.data.intValues[0]
        if (success === 1) {
          this.$Message.info('导入excel成功!!!')
          this.onSearch()
        }
      })
    },
    onExport () {
      // 导出EXCEL
      getAllGameJackPots().then((data) => {
        if (!data) {
          return
        }

        let allTableData = []
        let GameJackPotVO = getVO('GameJackPotVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = GameJackPotVO.decode(pv.data[i])
          vo.initBaseJackpot = vo.initBaseJackpot.toNumber()
          allTableData.push(vo)
        }
        console.log(allTableData)
        exportExcel(this.filename, allTableData)
      })
    },
    onFileChange (e) {
      var files = e.target.files || e.dataTransfer.files
      if (!files.length) {
        return
      }
      importExcel(e.target, this.setResults)
    },
    setResults (res) {
      this.results = res
      console.log('this.results:', this.results)
    }
  },
  computed: {
    tableColumns () {
      let cols = [
        {
          title: 'jackpotId',
          key: 'jackpotId',
          align: 'center',
          fixed: 'left'
        },
        {
          title: 'type',
          key: 'type',
          align: 'center',
          fixed: 'left'
        },
        {
          title: 'rate',
          key: 'rate',
          align: 'center'
        },
        {
          title: 'clientRate',
          key: 'clientRate',
          align: 'center'
        },
        {
          title: 'minRate',
          key: 'minRate',
          align: 'center'
        },
        {
          title: 'maxRate',
          key: 'maxRate',
          align: 'center'
        },
        {
          title: 'initBaseJackpot',
          key: 'initBaseJackpot',
          align: 'center'
        },
        {
          title: 'rewardRate',
          key: 'rewardRate',
          align: 'center'
        },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          fixed: 'right',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                on: {
                  click: () => {
                    let idx = params.index
                    this.showEditGameJackPot(idx)
                  }
                }
              }, '编辑')
            ])
          }
        }
      ]
      return cols
    }
  }
}

</script>

<style scoped>
.log-str{
  font-size: 14px;
  margin-bottom: 2px;
}
.content{
  min-height: 460px;
}
.bottom-page{
  margin-top: 5px;
  margin-bottom: 10px;
  float: right;
}

.bottom-table{
  margin-top: 5px;
  margin-bottom: 10px;
}
</style>
